﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Korbit;
using Korbit.Constant;
using Korbit.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Korbit.Common
{
    public class WalletComm
    {
        WalletInfo _WalletInfo;
        WalletDeposit _WalletDeposit;
        KorbitAccessInfo _AccessInfo;
        
        public WalletComm()
        {
            _WalletInfo = new WalletInfo();
            _WalletDeposit = new WalletDeposit();
        }

        public WalletComm(KorbitAccessInfo AccessInfo)
        {
            _AccessInfo = AccessInfo;
            _WalletInfo = new WalletInfo();
            _WalletDeposit = new WalletDeposit();
        }

        public WalletDeposit GetWalletDeposit()
        {
            return _WalletDeposit;
        }

        public WalletInfo GetWalletInfo()
        {
            return _WalletInfo;
        }

        public void RetrieveWallet()
        {
            if (!_AccessInfo.IsLogin())
                return;

            string strData;
            string strURL;
            string strParameter;

            Dictionary<string, string> dicURLParameter = _AccessInfo.GetURLParameterAccessKey();

            strURL = _AccessInfo.GetBaseURL() + KorbitURL.URL_WALLET;
            strParameter = CommonFunction.ConvertURLParameter(dicURLParameter);

            strURL = strURL + "?" + strParameter;
            strParameter = "";

            strData = Communication.GetRequestData(strURL, strParameter);

            SetWalletInfo(strData);

        }

        private void SetWalletInfo(string strData)
        {
            
            JObject objData;

            if (strData.Length == 0)
                return;

            objData = JObject.Parse(strData);

            // 계좌 정보를 추출
            JArray arrObj = (JArray)objData["in"];

            foreach(JToken item in arrObj)
            {
                if (item["currency"].ToString() == "krw")
                {
                    JToken oSubItem = item["address"];
                    _WalletInfo.In_KRW_BankName = oSubItem["bank"].ToString();
                    _WalletInfo.In_KRW_Account = oSubItem["account"].ToString();
                    _WalletInfo.In_KRW_Owner = oSubItem["owner"].ToString();
                }
                else if (item["currency"].ToString() == "btc")
                {
                    JToken oSubItem = item["address"];
                    _WalletInfo.In_BTC_Address = oSubItem["address"].ToString();
                }
            }


            //계좌 잔고 정보를 추출
            arrObj = (JArray)objData["balance"];

            foreach (JToken item in arrObj)
            {
                if (item["currency"].ToString() == "krw")
                {
                    _WalletDeposit.Total_KRW = Double.Parse(item["value"].ToString());
                }
                else if (item["currency"].ToString() == "btc")
                {
                    _WalletDeposit.Total_BTC = Double.Parse(item["value"].ToString());
            //계좌 잔고 정보를 추출
            arrObj = (JArray)objData["available"];
                }
            }


            foreach (JToken item in arrObj)
            {
                if (item["currency"].ToString() == "krw")
                {
                    _WalletDeposit.Available_KRW = Double.Parse(item["value"].ToString());
                }
                else if (item["currency"].ToString() == "btc")
                {
                    _WalletDeposit.Available_BTC = Double.Parse(item["value"].ToString());
                }
            }



            //_AccountInfo.In_KRW_BankName = objData["in"];

        }

    }


}
